//
//  JFAFNInstance.m
//  JFBase
//
//  Created by Alan Wang on 2018/8/17.
//  Copyright © 2018年 Alan Wang. All rights reserved.
//

#import "JFAFNInstance.h"
#import "JFDataParserHelper.h"
#import "JFCacheManager.h"
#import "JFResultVo.h"
#import "JFLogHelper.h"
#import "JFStringHelper.h"
#import "JFBasePri.h"
#if __has_include(<AFNetworking/AFNetworking.h>)
#import <AFNetworking/AFNetworking.h>
#else
#import "AFNetworking.h"
#endif
#import "JFInternationalHelper.h"
#import "JFThreadSafeDictionary.h"


 static AFHTTPSessionManager * instanceSessionManager;

@implementation JFAFNInstance
{
    //请求对列
//    NSMutableDictionary *_requestQueue;
     JFThreadSafeDictionary *_requestQueue;
    
}
+(instancetype )shareInstance
{
    static dispatch_once_t once;
    static id instance;
    dispatch_once(&once, ^{
        instance = [[self alloc]init];
        instanceSessionManager = [AFHTTPSessionManager manager];
    });
    return instance;
}

-(id)init
{
    self = [super init];
    if (self)
    {
//         _requestQueue=[[NSMutableDictionary alloc] init];
        _requestQueue = [[JFThreadSafeDictionary alloc] init];
    }
    return self;
}
-(void)invoke:(JFRequestVo *)reqParamVo callBack:(JFCallBack)callback{
    [super invoke:reqParamVo callBack:callback];
    
    switch (reqParamVo.methodtype) {
        case MethodType_Get:
        {
            [self get:reqParamVo callBack:callback];
        }break;
        case MethodType_Post:
         {
            [self post:reqParamVo callBack:callback];
         }break;
        case MethodType_Put:
         {
             [self put:reqParamVo callBack:callback];
         }break;
        case MethodType_Head:
         {
              [self head:reqParamVo callBack:callback];
         }break;
        case MethodType_Patch:
         {
             [self patch:reqParamVo callBack:callback];
         }break;
        case MethodType_Delete:
         {
           [self delete:reqParamVo callBack:callback];
         }break;
            
        default:
             [self post:reqParamVo callBack:callback];
            break;
    }
}

#pragma mark--内部方法

-(void)get:(JFRequestVo*)data callBack:(JFCallBack)callback{
    AFHTTPSessionManager *manager =[self getDefaultAFHTTPSessionManger:data];
    manager.completionQueue = dispatch_get_global_queue(0, 0);
    __weak __typeof(self) weakSelf=self;
    NSURLSessionDataTask* task=[manager GET:data.url parameters:data.reqParam headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        JFResultVo * dict=nil;
        if(data.dataparserServiceName){
            Class class = NSClassFromString(data.dataparserServiceName);
            if([class respondsToSelector:@selector(dataParser:dataParserType: requestVo:)]){
                dict = [class dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
            }else if ([class respondsToSelector:@selector(dataParser:dataParserType:)]){
                 dict = [class dataParser:responseObject dataParserType:data.dataparsertype];
            }else{
                dict = [JFDataParserHelper dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
            }
            
        }else{
             dict = [JFDataParserHelper dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
        }
        
       
        switch (data.cacheType) {
            case CacheType_CacheOrNet_Update:
            case CacheType_CacheAndNet_Update:{
                if (dict) {
                        if(dict.errorNo == 0 && data.flowNo){
                           [JFCacheManager sendDataToCache:dict key:data.flowNo];
                        }
                }
            }break;
            default:
                break;
        }
        if(!dict){
            dict = [[JFResultVo alloc] init];
            dict.errorNo =-999;
            dict.errorInfo = JFBaseLocalizedString(@"net.nodata");
        }
        if (data.isBackInMain) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (callback) {
                    callback(dict);
                }
            });
        }else{
            if (callback) {
                callback(dict);
            }
        }
        
        //把系统的内容清楚掉
        [weakSelf clearTask:data.flowNo];
        //debug的时候输出请求结束时间
        [weakSelf displayTime:data];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString * errorMsg = nil;
        if(data.dataparserServiceName){
            Class class = NSClassFromString(data.dataparserServiceName);
            if([class respondsToSelector:@selector(errorCodeToErrorMsg:)]){
                errorMsg = [class errorCodeToErrorMsg:error.code];
            }else{
                errorMsg = [JFDataParserHelper errorCodeToErrorMsg:error.code];
            }
        }else{
            errorMsg =[JFDataParserHelper errorCodeToErrorMsg:error.code];
        }
        if ([JFStringHelper isEmpty:errorMsg]) {
            errorMsg =  [error description];
        }
        JFResultVo * resustvo=[[JFResultVo alloc] init];
        dispatch_async(dispatch_get_main_queue(), ^{
            id datacc = error.userInfo[@"com.alamofire.serialization.response.error.data"];
            if (datacc) {
                id dict =  [NSJSONSerialization JSONObjectWithData: datacc options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
                NSMutableDictionary * tempDict = [[NSMutableDictionary alloc] init];
                tempDict[@"data"] = dict;
                
                [[NSNotificationCenter defaultCenter] postNotificationName:AW_NETWORK_ERRORRESONE object:tempDict];
                resustvo.results = tempDict;
            }
        });
        
        
        if(data.isDebug){
                  NSString * responce = [NSString stringWithFormat:@"--------请求响应begin---------\n接口:%@\n ShowError(%@)\n Error(%@)\n --------请求响应end-------",data.url,errorMsg,error.description];
                  [JFLogHelper stringSyncWriteLogToConsole:responce];
        }
        
      
        resustvo.errorNo=-1;
        resustvo.errorInfo = errorMsg;
        if (data.isBackInMain) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (callback) {
                    callback(resustvo);
                }
            });
        }else{
            if (callback) {
                callback(resustvo);
            }
        }
        //把系统的内容清楚掉
        [weakSelf clearTask:data.flowNo];
        
    }];
    
    [_requestQueue  setObject:task forKey:data.flowNo];
    
}
-(void)post:(JFRequestVo*)data callBack:(JFCallBack)callback{
    __weak __typeof(self) weakSelf=self;
    AFHTTPSessionManager *manager =[self getDefaultAFHTTPSessionManger:data];
    manager.completionQueue = dispatch_get_global_queue(0, 0);
    NSURLSessionDataTask* task=[manager POST:data.url parameters:data.reqParam headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
      
        
        JFResultVo * dict=nil;
        if(data.dataparserServiceName){
            Class class = NSClassFromString(data.dataparserServiceName);
            if([class respondsToSelector:@selector(dataParser:dataParserType: requestVo:)]){
                dict = [class dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
            }else if ([class respondsToSelector:@selector(dataParser:dataParserType:)]){
                dict = [class dataParser:responseObject dataParserType:data.dataparsertype];
            }else{
                dict = [JFDataParserHelper dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
            }
            
        }else{
            dict = [JFDataParserHelper dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
        }
        

        switch (data.cacheType) {
            case CacheType_CacheOrNet_Update:
            case CacheType_CacheAndNet_Update:{
                if (dict) {
                    if(dict.errorNo == 0 && data.flowNo){
                       [JFCacheManager sendDataToCache:dict key:data.flowNo];
                    }
                }
            }break;
            default:
                break;
        }
        NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
        NSDictionary *allHeaders = response.allHeaderFields;
        if(allHeaders){
            NSString * xauthtoken  = allHeaders[@"x-auth-token"];
            dict.noShowErrorInfo = xauthtoken;
        }
        //debug的时候输出请求结束时间
        [weakSelf displayTime:data];
        
        if(data.isDebug){
            NSString * responce = [NSString stringWithFormat:@"--------请求响应begin---------\n接口:%@\n返回结果(%@)\n --------请求响应end-------",data.url,responseObject];
            [JFLogHelper stringSyncWriteLogToConsole:responce];
        }

        if(!dict){
            dict = [[JFResultVo alloc] init];
            dict.errorNo =-999;
            dict.errorInfo = JFBaseLocalizedString(@"net.nodata");
        }
        
        //把系统的内容清楚掉
        [weakSelf clearTask:data.flowNo];
     
        
        if (data.isBackInMain) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (callback) {
                    callback(dict);
                }
            });
        }else{
            if (callback) {
                callback(dict);
            }
        }
        

        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString * errorMsg = nil;
        if(data.dataparserServiceName){
            Class class = NSClassFromString(data.dataparserServiceName);
            if([class respondsToSelector:@selector(errorCodeToErrorMsg:)]){
                errorMsg = [class errorCodeToErrorMsg:error.code];
            }else{
                errorMsg = [JFDataParserHelper errorCodeToErrorMsg:error.code];
            }
        }else{
            errorMsg =[JFDataParserHelper errorCodeToErrorMsg:error.code];
        }
        if ([JFStringHelper isEmpty:errorMsg]) {
            errorMsg =  [error description];
        }
    
        
        JFResultVo * resustvo=[[JFResultVo alloc] init];
        resustvo.errorNo=-1;
        resustvo.errorInfo= errorMsg;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            id datacc = error.userInfo[@"com.alamofire.serialization.response.error.data"];
            if (datacc) {
                id dict =  [NSJSONSerialization JSONObjectWithData: datacc options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
                NSMutableDictionary * tempDict = [[NSMutableDictionary alloc] init];
                tempDict[@"data"] = dict;
                
                [[NSNotificationCenter defaultCenter] postNotificationName:AW_NETWORK_ERRORRESONE object:dict];
                resustvo.results = tempDict;
            }
        });
        
        
        if(data.isDebug){
                NSString * responce = [NSString stringWithFormat:@"--------请求响应begin---------\n接口:%@\n ShowError(%@)\n Error(%@)\n --------请求响应end-------",data.url,errorMsg,error.description];
                [JFLogHelper stringSyncWriteLogToConsole:responce];
         }
        
        
        //把系统的内容清楚掉
        [weakSelf clearTask:data.flowNo];
        //debug的时候输出请求结束时间
        [weakSelf displayTime:data];
        
        if (data.isBackInMain) {
          dispatch_async(dispatch_get_main_queue(), ^{
             if (callback) {
                 callback(resustvo);
             }
          });
        }else{
            if (callback) {
                callback(resustvo);
            }
        }

        
    }];
    [_requestQueue  setObject:task forKey:data.flowNo]; 
}

-(void)head:(JFRequestVo*)data callBack:(JFCallBack)callback{
    __weak __typeof(self) weakSelf=self;
    AFHTTPSessionManager *manager =[self getDefaultAFHTTPSessionManger:data];
    manager.completionQueue = dispatch_get_global_queue(0, 0);
    NSURLSessionDataTask* task=[manager HEAD:data.url parameters:data.reqParam headers:nil  success:^(NSURLSessionDataTask * _Nonnull task) {
        JFResultVo * dict=nil;
        //debug的时候输出请求结束时间
        [weakSelf displayTime:data];
        
        if(data.isDebug){
            NSString * responce = [NSString stringWithFormat:@"--------请求响应begin---------\n接口:%@\n --------请求响应end-------",data.url];
            [JFLogHelper stringSyncWriteLogToConsole:responce];
        }

        if(!dict){
            dict = [[JFResultVo alloc] init];
            dict.errorNo =-999;
            dict.errorInfo = JFBaseLocalizedString(@"net.nodata");
        }
        
        //把系统的内容清楚掉
        [weakSelf clearTask:data.flowNo];
     
        
        if (data.isBackInMain) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (callback) {
                    callback(dict);
                }
            });
        }else{
            if (callback) {
                callback(dict);
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString * errorMsg = nil;
        if(data.dataparserServiceName){
            Class class = NSClassFromString(data.dataparserServiceName);
            if([class respondsToSelector:@selector(errorCodeToErrorMsg:)]){
                errorMsg = [class errorCodeToErrorMsg:error.code];
            }else{
                errorMsg = [JFDataParserHelper errorCodeToErrorMsg:error.code];
            }
        }else{
            errorMsg =[JFDataParserHelper errorCodeToErrorMsg:error.code];
        }
        if ([JFStringHelper isEmpty:errorMsg]) {
            errorMsg =  [error description];
        }
        
        
        JFResultVo * resustvo=[[JFResultVo alloc] init];
        resustvo.errorNo=-1;
        resustvo.errorInfo= errorMsg;
        
       dispatch_async(dispatch_get_main_queue(), ^{
           id datacc = error.userInfo[@"com.alamofire.serialization.response.error.data"];
           if (datacc) {
               id dict =  [NSJSONSerialization JSONObjectWithData: datacc options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
               NSMutableDictionary * tempDict = [[NSMutableDictionary alloc] init];
               tempDict[@"data"] = dict;
               
               [[NSNotificationCenter defaultCenter] postNotificationName:AW_NETWORK_ERRORRESONE object:tempDict];
               resustvo.results = tempDict;
           }
       });
        
        if(data.isDebug){
                NSString * responce = [NSString stringWithFormat:@"--------请求响应begin---------\n接口:%@\n ShowError(%@)\n Error(%@)\n --------请求响应end-------",data.url,errorMsg,error.description];
                [JFLogHelper stringSyncWriteLogToConsole:responce];
         }
        
        
        //把系统的内容清楚掉
        [weakSelf clearTask:data.flowNo];
        //debug的时候输出请求结束时间
        [weakSelf displayTime:data];
        
        if (data.isBackInMain) {
          dispatch_async(dispatch_get_main_queue(), ^{
             if (callback) {
                 callback(resustvo);
             }
          });
        }else{
            if (callback) {
                callback(resustvo);
            }
        }

        
    }];
    [_requestQueue  setObject:task forKey:data.flowNo];
}


-(void)put:(JFRequestVo*)data callBack:(JFCallBack)callback{
    __weak __typeof(self) weakSelf=self;
    AFHTTPSessionManager *manager =[self getDefaultAFHTTPSessionManger:data];
    manager.completionQueue = dispatch_get_global_queue(0, 0);
    NSURLSessionDataTask* task=[manager PUT:data.url parameters:data.reqParam headers:nil  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        JFResultVo * dict=nil;
        if(data.dataparserServiceName){
            Class class = NSClassFromString(data.dataparserServiceName);
            if([class respondsToSelector:@selector(dataParser:dataParserType: requestVo:)]){
                dict = [class dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
            }else if ([class respondsToSelector:@selector(dataParser:dataParserType:)]){
                dict = [class dataParser:responseObject dataParserType:data.dataparsertype];
            }else{
                dict = [JFDataParserHelper dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
            }
            
        }else{
            dict = [JFDataParserHelper dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
        }
        

        switch (data.cacheType) {
            case CacheType_CacheOrNet_Update:
            case CacheType_CacheAndNet_Update:{
                if (dict) {
                    if(dict.errorNo == 0 && data.flowNo){
                       [JFCacheManager sendDataToCache:dict key:data.flowNo];
                    }
                }
            }break;
            default:
                break;
        }
        //debug的时候输出请求结束时间
        [weakSelf displayTime:data];
        
        
        
        if(data.isDebug){
            NSString * responce = [NSString stringWithFormat:@"--------请求响应begin---------\n接口:%@\n返回结果(%@)\n --------请求响应end-------",data.url,responseObject];
            [JFLogHelper stringSyncWriteLogToConsole:responce];
        }

        if(!dict){
            dict = [[JFResultVo alloc] init];
            dict.errorNo =-999;
            dict.errorInfo = JFBaseLocalizedString(@"net.nodata");
        }
        
        //把系统的内容清楚掉
        [weakSelf clearTask:data.flowNo];
     
        
        if (data.isBackInMain) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (callback) {
                    callback(dict);
                }
            });
        }else{
            if (callback) {
                callback(dict);
            }
        }
        

        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString * errorMsg = nil;
        if(data.dataparserServiceName){
            Class class = NSClassFromString(data.dataparserServiceName);
            if([class respondsToSelector:@selector(errorCodeToErrorMsg:)]){
                errorMsg = [class errorCodeToErrorMsg:error.code];
            }else{
                errorMsg = [JFDataParserHelper errorCodeToErrorMsg:error.code];
            }
        }else{
            errorMsg =[JFDataParserHelper errorCodeToErrorMsg:error.code];
        }
        if ([JFStringHelper isEmpty:errorMsg]) {
            errorMsg =  [error description];
        }
        
        
        JFResultVo * resustvo=[[JFResultVo alloc] init];
        resustvo.errorNo=-1;
        resustvo.errorInfo= errorMsg;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            id datacc = error.userInfo[@"com.alamofire.serialization.response.error.data"];
            if (datacc) {
                id dict =  [NSJSONSerialization JSONObjectWithData: datacc options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
                NSMutableDictionary * tempDict = [[NSMutableDictionary alloc] init];
                tempDict[@"data"] = dict;
                
                [[NSNotificationCenter defaultCenter] postNotificationName:AW_NETWORK_ERRORRESONE object:tempDict];
                resustvo.results = tempDict;
            }
        });
        if(data.isDebug){
                NSString * responce = [NSString stringWithFormat:@"--------请求响应begin---------\n接口:%@\n ShowError(%@)\n Error(%@)\n --------请求响应end-------",data.url,errorMsg,error.description];
                [JFLogHelper stringSyncWriteLogToConsole:responce];
         }
        
        
        //把系统的内容清楚掉
        [weakSelf clearTask:data.flowNo];
        //debug的时候输出请求结束时间
        [weakSelf displayTime:data];
        
        if (data.isBackInMain) {
          dispatch_async(dispatch_get_main_queue(), ^{
             if (callback) {
                 callback(resustvo);
             }
          });
        }else{
            if (callback) {
                callback(resustvo);
            }
        }

        
    }];
    [_requestQueue  setObject:task forKey:data.flowNo];
}

-(void)patch:(JFRequestVo*)data callBack:(JFCallBack)callback{
    __weak __typeof(self) weakSelf=self;
    AFHTTPSessionManager *manager =[self getDefaultAFHTTPSessionManger:data];
    manager.completionQueue = dispatch_get_global_queue(0, 0);
    NSURLSessionDataTask* task=[manager PATCH:data.url parameters:data.reqParam headers:nil  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        JFResultVo * dict=nil;
        if(data.dataparserServiceName){
            Class class = NSClassFromString(data.dataparserServiceName);
            if([class respondsToSelector:@selector(dataParser:dataParserType: requestVo:)]){
                dict = [class dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
            }else if ([class respondsToSelector:@selector(dataParser:dataParserType:)]){
                dict = [class dataParser:responseObject dataParserType:data.dataparsertype];
            }else{
                dict = [JFDataParserHelper dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
            }
            
        }else{
            dict = [JFDataParserHelper dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
        }
        

        switch (data.cacheType) {
            case CacheType_CacheOrNet_Update:
            case CacheType_CacheAndNet_Update:{
                if (dict) {
                    if(dict.errorNo == 0 && data.flowNo){
                       [JFCacheManager sendDataToCache:dict key:data.flowNo];
                    }
                }
            }break;
            default:
                break;
        }
        //debug的时候输出请求结束时间
        [weakSelf displayTime:data];
        
        if(data.isDebug){
            NSString * responce = [NSString stringWithFormat:@"--------请求响应begin---------\n接口:%@\n返回结果(%@)\n --------请求响应end-------",data.url,responseObject];
            [JFLogHelper stringSyncWriteLogToConsole:responce];
        }

        if(!dict){
            dict = [[JFResultVo alloc] init];
            dict.errorNo =-999;
            dict.errorInfo = JFBaseLocalizedString(@"net.nodata");
        }
        
        //把系统的内容清楚掉
        [weakSelf clearTask:data.flowNo];
     
        
        if (data.isBackInMain) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (callback) {
                    callback(dict);
                }
            });
        }else{
            if (callback) {
                callback(dict);
            }
        }
        

        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString * errorMsg = nil;
        if(data.dataparserServiceName){
            Class class = NSClassFromString(data.dataparserServiceName);
            if([class respondsToSelector:@selector(errorCodeToErrorMsg:)]){
                errorMsg = [class errorCodeToErrorMsg:error.code];
            }else{
                errorMsg = [JFDataParserHelper errorCodeToErrorMsg:error.code];
            }
        }else{
            errorMsg =[JFDataParserHelper errorCodeToErrorMsg:error.code];
        }
        if ([JFStringHelper isEmpty:errorMsg]) {
            errorMsg =  [error description];
        }
        
        
        JFResultVo * resustvo=[[JFResultVo alloc] init];
        resustvo.errorNo=-1;
        resustvo.errorInfo= errorMsg;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            id datacc = error.userInfo[@"com.alamofire.serialization.response.error.data"];
            if (datacc) {
                id dict =  [NSJSONSerialization JSONObjectWithData: datacc options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
                NSMutableDictionary * tempDict = [[NSMutableDictionary alloc] init];
                tempDict[@"data"] = dict;
                
                [[NSNotificationCenter defaultCenter] postNotificationName:AW_NETWORK_ERRORRESONE object:tempDict];
                resustvo.results = tempDict;
            }
        });
        
        if(data.isDebug){
                NSString * responce = [NSString stringWithFormat:@"--------请求响应begin---------\n接口:%@\n ShowError(%@)\n Error(%@)\n --------请求响应end-------",data.url,errorMsg,error.description];
                [JFLogHelper stringSyncWriteLogToConsole:responce];
         }
        
        
        //把系统的内容清楚掉
        [weakSelf clearTask:data.flowNo];
        //debug的时候输出请求结束时间
        [weakSelf displayTime:data];
        
        if (data.isBackInMain) {
          dispatch_async(dispatch_get_main_queue(), ^{
             if (callback) {
                 callback(resustvo);
             }
          });
        }else{
            if (callback) {
                callback(resustvo);
            }
        }

        
    }];
    [_requestQueue  setObject:task forKey:data.flowNo];
}
-(void)delete:(JFRequestVo*)data callBack:(JFCallBack)callback{
    __weak __typeof(self) weakSelf=self;
    AFHTTPSessionManager *manager =[self getDefaultAFHTTPSessionManger:data];
    manager.completionQueue = dispatch_get_global_queue(0, 0);
    NSURLSessionDataTask* task=[manager DELETE:data.url parameters:data.reqParam headers:nil  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        JFResultVo * dict=nil;
        if(data.dataparserServiceName){
            Class class = NSClassFromString(data.dataparserServiceName);
            if([class respondsToSelector:@selector(dataParser:dataParserType: requestVo:)]){
                dict = [class dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
            }else if ([class respondsToSelector:@selector(dataParser:dataParserType:)]){
                dict = [class dataParser:responseObject dataParserType:data.dataparsertype];
            }else{
                dict = [JFDataParserHelper dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
            }
            
        }else{
            dict = [JFDataParserHelper dataParser:responseObject dataParserType:data.dataparsertype requestVo:data];
        }
        

        switch (data.cacheType) {
            case CacheType_CacheOrNet_Update:
            case CacheType_CacheAndNet_Update:{
                if (dict) {
                    if(dict.errorNo == 0 && data.flowNo){
                       [JFCacheManager sendDataToCache:dict key:data.flowNo];
                    }
                }
            }break;
            default:
                break;
        }
        //debug的时候输出请求结束时间
        [weakSelf displayTime:data];
        
        if(data.isDebug){
            NSString * responce = [NSString stringWithFormat:@"--------请求响应begin---------\n接口:%@\n返回结果(%@)\n --------请求响应end-------",data.url,responseObject];
            [JFLogHelper stringSyncWriteLogToConsole:responce];
        }

        if(!dict){
            dict = [[JFResultVo alloc] init];
            dict.errorNo =-999;
            dict.errorInfo = JFBaseLocalizedString(@"net.nodata");
        }
        
        //把系统的内容清楚掉
        [weakSelf clearTask:data.flowNo];
     
        
        if (data.isBackInMain) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (callback) {
                    callback(dict);
                }
            });
        }else{
            if (callback) {
                callback(dict);
            }
        }
        

        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString * errorMsg = nil;
        if(data.dataparserServiceName){
            Class class = NSClassFromString(data.dataparserServiceName);
            if([class respondsToSelector:@selector(errorCodeToErrorMsg:)]){
                errorMsg = [class errorCodeToErrorMsg:error.code];
            }else{
                errorMsg = [JFDataParserHelper errorCodeToErrorMsg:error.code];
            }
        }else{
            errorMsg =[JFDataParserHelper errorCodeToErrorMsg:error.code];
        }
        if ([JFStringHelper isEmpty:errorMsg]) {
            errorMsg =  [error description];
        }
        
        
        JFResultVo * resustvo=[[JFResultVo alloc] init];
        resustvo.errorNo=-1;
        resustvo.errorInfo= errorMsg;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            id datacc = error.userInfo[@"com.alamofire.serialization.response.error.data"];
            if (datacc) {
                id dict =  [NSJSONSerialization JSONObjectWithData: datacc options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
                NSMutableDictionary * tempDict = [[NSMutableDictionary alloc] init];
                tempDict[@"data"] = dict;
                
                [[NSNotificationCenter defaultCenter] postNotificationName:AW_NETWORK_ERRORRESONE object:tempDict];
                resustvo.results = tempDict;
            }
        });
        
        if(data.isDebug){
                NSString * responce = [NSString stringWithFormat:@"--------请求响应begin---------\n接口:%@\n ShowError(%@)\n Error(%@)\n --------请求响应end-------",data.url,errorMsg,error.description];
                [JFLogHelper stringSyncWriteLogToConsole:responce];
         }
        
        
        //把系统的内容清楚掉
        [weakSelf clearTask:data.flowNo];
        //debug的时候输出请求结束时间
        [weakSelf displayTime:data];
        
        if (data.isBackInMain) {
          dispatch_async(dispatch_get_main_queue(), ^{
             if (callback) {
                 callback(resustvo);
             }
          });
        }else{
            if (callback) {
                callback(resustvo);
            }
        }

        
    }];
    [_requestQueue  setObject:task forKey:data.flowNo];
}
/**
 *  @author Alan, 16-12-23 11:12:50
 *
 *  @brief 网络请求预处理 根据流水号判断是否需要发起这次请求
 *
 *  @param flowNo 请求流水号
 */
-(void)pretreatment:(NSString*)flowNo data:(JFRequestVo*)data{
    if (_requestQueue) {
        NSArray* dataArr = _requestQueue.allKeys;
        if (dataArr&&[dataArr containsObject:flowNo]) {
            if (data.isDebug) {
                NSLog(@"重复网络请求,已取消该次请求");
            }
            
            return;
        }
    }
}


-(void)cancelAllRequest{
    if (_requestQueue) {
        //取消所有的请求
        NSArray * allRequest = [_requestQueue allValues];
        if(allRequest && allRequest.count){
            for (NSURLSessionDataTask* task in allRequest) {
                [task cancel];
            }
            [_requestQueue removeAllObjects];
        }
    }
}

-(void)clearRequest:(NSString *)flowNo
{
    NSURLSessionDataTask* task=[_requestQueue objectForKey:flowNo];
    if(task){
        [task cancel];
        [_requestQueue removeObjectForKey:flowNo];
    }
}

- (void)clearAllRequest{
    [self cancelAllRequest];
}

-(void)clearRequest:(NSString *)flowNo baseUri:(NSString*)url{
    [self clearRequest:flowNo];
}

#pragma mark--公共方法
-(AFHTTPSessionManager*)getDefaultAFHTTPSessionManger:(JFRequestVo*)requestVo{
    AFHTTPSessionManager *manager = instanceSessionManager;
    if(!manager){
         manager = [AFHTTPSessionManager manager];
    }
    AFHTTPResponseSerializer * responseSerializer = [AFJSONResponseSerializer serializer];
    AFHTTPRequestSerializer * requestSerializer  = [AFJSONRequestSerializer serializer];
    
    switch (requestVo.dataparsertype) {
        case DataParserType_JSON:
        {
            responseSerializer = [AFJSONResponseSerializer serializer];
            requestSerializer = [AFJSONRequestSerializer serializer];
        }
            break;
        case DataParserType_XML:
        {
            responseSerializer = [AFXMLParserResponseSerializer serializer];
            requestSerializer = [AFJSONRequestSerializer serializer];
        }break;
        default:{
            responseSerializer = [AFHTTPResponseSerializer serializer];
            requestSerializer = [AFJSONRequestSerializer serializer];
        }break;
    }
    
    //获取超时时间
    if(requestVo.timeOut>0){
        requestSerializer.timeoutInterval = requestVo.timeOut;
    }else{
        requestSerializer.timeoutInterval = THtimeoutInterval;
    }
    manager.responseSerializer = responseSerializer;
    manager.requestSerializer = requestSerializer;
    
    if(requestVo.header){
        NSArray * arr = [requestVo.header allKeys];
        for (NSString * key in arr) {
            id vaule = requestVo.header[key];
            if(vaule){
                [manager.requestSerializer setValue:vaule forHTTPHeaderField:key];
            }
        }
    }
    //新增语言参数
    [manager.requestSerializer setValue:[JFInternationalHelper currentLanguage] forHTTPHeaderField:@"Lang"];

    
    
    if(requestVo.security){
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
        switch (requestVo.cerCheckMode) {
            case CerCheckMode_None:
                securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
                break;
            case CerCheckMode_PublicKey:
                  securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey];
                break;
            case CerCheckMode_Certificate:
                  securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
                break;
            default:
                break;
        }
        securityPolicy.validatesDomainName = requestVo.validatesDomainName;
        securityPolicy.allowInvalidCertificates = requestVo.allowInvalidCertificates;
        manager.securityPolicy = securityPolicy;
    }
    return manager;
}

-(void)displayTime:(JFRequestVo*)data{
    if (data.isDebug) {
        
        NSTimeInterval endTime = [[NSDate date]timeIntervalSince1970];
        NSString * timeStr = [NSString stringWithFormat:@"--------请求花费时间begin---------\n接口:%@\n花费时间(%fs)\n请求结束\n --------请求花费时间end-------",data.url,(endTime - data.beginTime)];
        [JFLogHelper stringSyncWriteLogToConsole:timeStr];
    }
}

-(void)clearTask:(NSString*)flowNo{
    [_requestQueue removeObjectForKey:flowNo];
}
@end
